package com.gather.treemaps;

import com.gather.orm.House;

import java.util.*;

public class Example02 {
    public static void main(String[] args) {
        System.out.println("测试去重排序问题");
        TreeMap<House, String> houseStringTreeMap = new TreeMap<>();
        House h1 = new House("17栋A",20);
        House h2 = new House("18栋C",30);
        House h3 = new House("1栋B",10);
        // 添加
        houseStringTreeMap.put(h1,"碧桂园");
        houseStringTreeMap.put(h2,"万达");
        houseStringTreeMap.put(h3,"恒大");
        houseStringTreeMap.put(h3,"恒大");
        houseStringTreeMap.put(new House("1栋B",10),"恒大");
        // {18栋C:30=万达, 17栋A:20=碧桂园, 1栋B:10=恒大}
        System.out.println(houseStringTreeMap.toString());
        System.out.println("---------");
        // 集合遍历操作
        Set<House> houses = houseStringTreeMap.keySet();
        for (House house : houses) {
            System.out.println(house);
            String s = houseStringTreeMap.get(house);
            System.out.println(s);
        }
        System.out.println("---------");

        // 使用迭代器遍历
        Collection<String> values = houseStringTreeMap.values();
        Iterator<String> iterator = values.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println("values "+ next);
        }
        System.out.println("---------");
        // 使用映射图遍历
        Set<Map.Entry<House, String>> entries = houseStringTreeMap.entrySet();
        for (Map.Entry<House, String> entry : entries) {
            // 获取 key
            House key = entry.getKey();
            System.out.println(key);
            System.out.println(key.getPrice());
            // 获取value
            String value = entry.getValue();
            System.out.println(value);
        }
    }
}
